﻿Public Class frmShipmentDetail

#Region "Method"

#Region "Overrides"
    ''' <summary>
    ''' Sub đưa dữ liệu hiển thị trên control
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Protected Overrides Sub BindingControl()
        With BusinessObject.dsFMS.Shipment
            LoadPartner()
            LoadEmployee()
            LoadCategory()
            txtShipmentAlias.DataBindings.Add("Text", bsFMS, .ShipmentAliasColumn.ColumnName)
            txtShipmentNumber.DataBindings.Add("Text", bsFMS, .ShipmentNumberColumn.ColumnName)
            dteReceiveDate.DataBindings.Add("Text", bsFMS, .ShipmentPurchaseDateColumn.ColumnName)
            dteShipmentDate.DataBindings.Add("Text", bsFMS, .ShipmentAddedDateColumn.ColumnName)
            txtDescription.DataBindings.Add("Text", bsFMS, .ShipmentDescriptionColumn.ColumnName)
            If ActionMode <> clscommon.ActionMode.Add Then
                rdbTaxRate.Value = bsFMS.Current(.ShipmentTaxColumn.ColumnName)
                cboReceiver.Value = bsFMS.Current(.ShipmentReceiveEmpColumn.ColumnName)
                cboImporter.Value = bsFMS.Current(.ShipmentImportEmpColumn.ColumnName)
                cboPartner.Value = bsFMS.Current(.PartnerIDColumn.ColumnName)
                drpRepresentative.Value = bsFMS.Current(.RepresentativeIDColumn.ColumnName)
            End If
            LoadShipmentDetail()
        End With
    End Sub

    ''' <summary>
    ''' Sub xóa binding dữ liệu trên control
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Protected Overrides Sub ClearDataBingding()
        With Me
            .dteShipmentDate.DataBindings.Clear()
            .dteReceiveDate.DataBindings.Clear()
            .txtShipmentAlias.DataBindings.Clear()
            .txtShipmentNumber.DataBindings.Clear()
            .txtDescription.DataBindings.Clear()
        End With
    End Sub

    ''' <summary>
    ''' Sub set thuộc tính cho các control là read only hay không
    ''' </summary>
    ''' <param name="isReadOnly">Ở chế độ read only thì đặt là true và ngược lại</param>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Protected Overrides Sub SetControlEditable(ByVal isReadOnly As Boolean)
        MyBase.SetControlEditable(isReadOnly)
        With Me
            .cboCategory.ButtonsRight(0).Visible = Not isReadOnly
            .cboCategory.ReadOnly = isReadOnly
            .drpItem.ButtonsRight(0).Visible = Not isReadOnly
            .drpItem.ReadOnly = isReadOnly
            .txtQuantity.ButtonsLeft(0).Visible = Not isReadOnly
            .txtQuantity.ButtonsRight(0).Visible = Not isReadOnly
            .txtQuantity.ReadOnly = isReadOnly
            .txtSellPrice.ReadOnly = isReadOnly
            .txtBuyPrice.ReadOnly = isReadOnly
            .btnAddShipmentDetail.Visible = Not isReadOnly
            .dteReceiveDate.ReadOnly = isReadOnly
            .txtShipmentNumber.ReadOnly = isReadOnly
            .dteShipmentDate.ReadOnly = isReadOnly
            .txtShipmentAlias.ReadOnly = isReadOnly
            .cboReceiver.ButtonsRight(0).Visible = Not isReadOnly
            .cboReceiver.ReadOnly = isReadOnly
            .cboImporter.ReadOnly = isReadOnly
            .cboPartner.ButtonsRight(0).Visible = Not isReadOnly
            .cboPartner.ReadOnly = isReadOnly
            .drpRepresentative.ButtonsRight(0).Visible = Not isReadOnly
            .drpRepresentative.ReadOnly = isReadOnly
            .rdbTaxRate.Enabled = Not isReadOnly
            .txtDescription.ReadOnly = isReadOnly
            If isReadOnly Then
                .grdShipmentDetail.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.CellSelect
            Else
                .grdShipmentDetail.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.EditAndSelectText
                If ActionMode = clscommon.ActionMode.Add Then
                    BusinessObject.dsFMS.ShipmentDetail.Clear()
                    .txtSellPrice.Text = String.Empty
                    .txtBuyPrice.Text = String.Empty
                    .txtQuantity.Text = 1
                    .cboCategory.SelectedIndex = 0
                    .cboReceiver.SelectedIndex = 0
                    .cboImporter.SelectedIndex = 0
                    .rdbTaxRate.CheckedIndex = 0
                End If
            End If
        End With
    End Sub

    ''' <summary>
    ''' Sub xác định vị trí của bản ghi hiện thời và load lại dữ liệu
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Protected Overrides Sub RefreshData()
        MyBase.RefreshData()
        With BusinessObject.dsFMS
            If Not IsNothing(bsFMS.Current) And ActionMode <> clscommon.ActionMode.Add Then
                LoadShipmentDetail()
                rdbTaxRate.Value = bsFMS.Current(.Shipment.ShipmentTaxColumn.ColumnName)
                cboReceiver.Value = bsFMS.Current(.Shipment.ShipmentReceiveEmpColumn.ColumnName)
                cboImporter.Value = bsFMS.Current(.Shipment.ShipmentImportEmpColumn.ColumnName)
                cboPartner.Value = bsFMS.Current(.Shipment.PartnerIDColumn.ColumnName)
                drpRepresentative.Value = bsFMS.Current(.Shipment.RepresentativeIDColumn.ColumnName)
            End If
        End With
    End Sub

    ''' <summary>
    ''' Sub đặt giá trị cho khóa ngoại
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 18/06/2012</remarks>
    Protected Overrides Sub SetDataValue()
        With BusinessObject
            .CurrentRow(.dsFMS.Shipment.PartnerIDColumn.ColumnName) = cboPartner.Value
            .CurrentRow(.dsFMS.Shipment.RepresentativeIDColumn.ColumnName) = drpRepresentative.Value
            .CurrentRow(.dsFMS.Shipment.ShipmentReceiveEmpColumn.ColumnName) = cboReceiver.Value
            .CurrentRow(.dsFMS.Shipment.ShipmentImportEmpColumn.ColumnName) = cboImporter.Value
            .CurrentRow(.dsFMS.Shipment.ShipmentTaxColumn.ColumnName) = rdbTaxRate.Value
        End With
    End Sub

    ''' <summary>
    ''' Hàm lưu dữ liệu vào database (add hoặc edit)
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Protected Overrides Sub SaveData()
        If grdShipmentDetail.Rows.Count > 0 Then
            bsShipmentDetail.EndEdit()
            MyBase.SaveData()
        Else
            clscommon.ShowInformation(My.Resources.NoItemInShipment)
        End If
    End Sub

    ''' <summary>
    ''' Sub xem phiếu nhập kho hiện tại
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 18/06/2012</remarks>
    Protected Overrides Sub ViewDetail()
        With BusinessObject
            '.dsXHGM.DisplayInvoice.Clear()
            '.FillReport(.dsXHGM.DisplayInvoice.TableName, bsXHGM.Current(.dsXHGM.Invoice.InvoiceIDColumn.ColumnName), Nothing, Nothing)
            'Dim viewInvoice As New frmInvoiceDetailView
            'With viewInvoice
            '    .DataSource = BusinessObject.dsXHGM.DisplayInvoice
            '    .Parameter = clsCommon.MoneyReader(Long.Parse(txtTotalAmount.Text))
            '    .Title = String.Format(My.Resources.InvoiceDetailTitle, dteDate.DateTime.Day.ToString & "/" & dteDate.DateTime.Month.ToString & "/" & dteDate.DateTime.Year.ToString, cboPlateNumber.Text)
            '    .Owner = Me
            '    .ShowDialog()
            'End With
        End With
    End Sub

    ''' <summary>
    ''' Sub in phiếu nhập kho hiện tại
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 18/06/2012</remarks>
    Protected Overrides Sub PrintDetail()
        With BusinessObject
            '.dsXHGM.DisplayInvoice.Clear()
            '.FillReport(.dsXHGM.DisplayInvoice.TableName, bsXHGM.Current(.dsXHGM.Invoice.InvoiceIDColumn.ColumnName), Nothing, Nothing)
            'Dim printInvoice As New frmInvoiceDetailPrint
            'With printInvoice
            '    .ReportType = clsCommon.ReportType.SmallInvoice
            '    .DataSource = BusinessObject.dsXHGM.DisplayInvoice
            '    .Parameter = clsCommon.MoneyReader(Long.Parse(txtTotalAmount.Text))
            '    .Owner = Me
            '    .ShowDialog()
            'End With
        End With
    End Sub
#End Region

    ''' <summary>
    ''' Sub đưa danh sách các mặt hàng trong lô hàng lên grdShipmentDetail
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Private Sub LoadShipmentDetail()
        With BusinessObject
            .dsFMS.ShipmentDetail.Clear()
            .GetDataByValue(.dsFMS.ShipmentDetail.TableName, .dsFMS.Shipment.ShipmentIDColumn.ColumnName, bsFMS.Current(.dsFMS.Shipment.ShipmentIDColumn.ColumnName))
            bsShipmentDetail.DataSource = .dsFMS
            bsShipmentDetail.DataMember = .dsFMS.ShipmentDetail.TableName
            grdShipmentDetail.DataSource = bsShipmentDetail
        End With
    End Sub

    ''' <summary>
    ''' Sub đưa danh sách nhân viên hiển thị lên cboReceiver và cboImporter
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 06/03/2012</remarks>
    Private Sub LoadEmployee()
        With BusinessObject
            .dsFMS.Employee.Clear()
            .GetTableData(.dsFMS.Employee.TableName)
            cboReceiver.DataSource = .dsFMS.Employee
            cboReceiver.DisplayMember = .dsFMS.Employee.EmployeeNameColumn.ColumnName
            cboReceiver.ValueMember = .dsFMS.Employee.EmployeeIDColumn.ColumnName
            cboImporter.DataSource = .dsFMS.Employee
            cboImporter.DisplayMember = .dsFMS.Employee.EmployeeNameColumn.ColumnName
            cboImporter.ValueMember = .dsFMS.Employee.EmployeeIDColumn.ColumnName
            If cboReceiver.Items.Count > 0 Then
                cboReceiver.SelectedIndex = 0
                cboImporter.SelectedIndex = 0
            Else
                cboReceiver.Text = String.Empty
                cboImporter.Text = String.Empty
            End If
        End With
    End Sub

    ''' <summary>
    ''' Sub lấy dữ liệu từ bảng Partner đưa vào dataset và hiển thị trên cboPartner
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2010</remarks>
    Private Sub LoadPartner()
        With BusinessObject
            .dsFMS.Partner.Clear()
            .GetTableData(.dsFMS.Partner.TableName)
            cboPartner.DataSource = .dsFMS.Partner
            cboPartner.DisplayMember = .dsFMS.Partner.PartnerNameColumn.ColumnName
            cboPartner.ValueMember = .dsFMS.Partner.PartnerIDColumn.ColumnName
            If cboPartner.Items.Count > 0 Then
                cboPartner.SelectedIndex = 0
                LoadRepresentative()
            Else
                cboPartner.Text = String.Empty
            End If
        End With
    End Sub

    ''' <summary>
    ''' Sub lấy dữ liệu từ bảng Representative đưa vào dataset và hiển thị trên drpRepresentative
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 12/06/2010</remarks>
    Private Sub LoadRepresentative()
        With BusinessObject
            .dsFMS.Representative.Clear()
            .GetDataByValue(.dsFMS.Representative.TableName, .dsFMS.Representative.PartnerIDColumn.ColumnName, cboPartner.Value)
            drpRepresentative.DataSource = .dsFMS.Representative
            drpRepresentative.DisplayMember = String.Format(My.Resources.TableInfo, .dsFMS.Representative.TableName)
            drpRepresentative.ValueMember = .dsFMS.Representative.RepresentativeIDColumn.ColumnName
            If drpRepresentative.Rows.Count > 0 Then
                drpRepresentative.SelectedRow = drpRepresentative.Rows(0)
            Else
                drpRepresentative.Text = String.Empty
            End If
        End With
    End Sub

    ''' <summary>
    ''' Sub lấy dữ liệu từ bảng Category đưa vào dataset và hiển thị trên cboCategory
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2010</remarks>
    Private Sub LoadCategory()
        With BusinessObject
            .dsFMS.Category.Clear()
            .GetTableData(BusinessObject.dsFMS.Category.TableName)
            cboCategory.DataSource = .dsFMS.Category
            cboCategory.DisplayMember = .dsFMS.Category.CategoryNameColumn.ColumnName
            cboCategory.ValueMember = .dsFMS.Category.CategoryIDColumn.ColumnName
            If cboCategory.Items.Count > 0 Then
                cboCategory.SelectedIndex = 0
                LoadItem()
            Else
                cboCategory.Text = String.Empty
            End If
        End With
    End Sub

    ''' <summary>
    ''' Sub đưa danh sách các mặt hàng lên drpItem
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Private Sub LoadItem()
        With BusinessObject
            .dsFMS.Item.Clear()
            .GetDataByValue(.dsFMS.Item.TableName, .dsFMS.Item.CategoryIDColumn.ColumnName, cboCategory.Value)
            drpItem.DataSource = .dsFMS.Item
            drpItem.DisplayMember = String.Format(My.Resources.TableInfo, .dsFMS.Item.TableName)
            drpItem.ValueMember = .dsFMS.Item.ItemIDColumn.ColumnName
            If drpItem.Rows.Count > 0 Then
                drpItem.SelectedRow = drpItem.Rows(0)
            Else
                drpItem.Text = String.Empty
            End If
        End With
    End Sub

    ''' <summary>
    ''' Hàm tạo mới một dòng trong bảng ShipmentDetail trên dataset
    ''' </summary>
    ''' <returns>Trả lại true nếu thành công và ngược lại</returns>
    ''' <remarks>Nguyễn Huy Thường added on 18/06/2012</remarks>
    Private Function AddShipmentDetail() As Boolean
        Try
            With BusinessObject.dsFMS
                If IsNothing(drpItem.SelectedRow) Then
                    drpItem.Focus()
                    clscommon.ShowInformation(String.Format(My.Resources.ItemNotSelected, "mặt hàng"))
                    Return False
                ElseIf Not clscommon.CheckQuantity(txtQuantity.Text) Then
                    txtQuantity.Focus()
                    txtQuantity.SelectAll()
                    Return False
                ElseIf Not clscommon.CheckMoney(txtBuyPrice.Text) Then
                    txtBuyPrice.Focus()
                    txtBuyPrice.SelectAll()
                    Return False
                ElseIf Not clscommon.CheckMoney(txtSellPrice.Text) Then
                    txtSellPrice.Focus()
                    txtSellPrice.SelectAll()
                    Return False
                Else
                    Dim itemAdded As Boolean
                    For Each row As Infragistics.Win.UltraWinGrid.UltraGridRow In grdShipmentDetail.Rows
                        If Not String.IsNullOrEmpty(row.Cells(.ShipmentDetail.ShipmentDetailIDColumn.ColumnName).Value.ToString()) Then
                            If row.Cells(.ShipmentDetail.ItemIDColumn.ColumnName).Value = drpItem.Value Then
                                row.Activate()
                                itemAdded = True
                            End If
                        End If
                    Next
                    If itemAdded Then
                        grdShipmentDetail.ActiveRow.Cells(.ShipmentDetail.ShipmentDetailQuantityColumn.ColumnName).Value += Decimal.Parse(txtQuantity.Text)
                        grdShipmentDetail.ActiveRow.Cells(.ShipmentDetail.ShipmentDetailBuyPriceColumn.ColumnName).Value = Decimal.Parse(txtBuyPrice.Text)
                        grdShipmentDetail.ActiveRow.Cells(.ShipmentDetail.ShipmentDetailSellPriceColumn.ColumnName).Value = Decimal.Parse(txtSellPrice.Text)
                        bsShipmentDetail.Position = grdShipmentDetail.ActiveRow.Index
                        Return True
                    Else
                        Dim drNewRow As BL.FMSDataSet.ShipmentDetailRow = .ShipmentDetail.NewShipmentDetailRow
                        With drNewRow
                            .ShipmentDetailID = Guid.NewGuid
                            .ShipmentID = BusinessObject.CurrentRow(BusinessObject.dsFMS.Shipment.ShipmentIDColumn.ColumnName)
                            .ItemID = drpItem.Value
                            .ShipmentDetailQuantity = Decimal.Parse(txtQuantity.Text)
                            .ShipmentDetailBuyPrice = Decimal.Parse(txtBuyPrice.Text)
                            .ShipmentDetailSellPrice = Decimal.Parse(txtSellPrice.Text)
                            .ShipmentDetailAddedDate = Today
                            .SetShipmentDetailDeletedDateNull()
                            .ShipmentDetailStatus = True
                            .SetShipmentDetailDescriptionNull()
                        End With
                        .ShipmentDetail.Rows.Add(drNewRow)
                        bsShipmentDetail.Position = grdShipmentDetail.Rows.Count - 1
                        Return True
                    End If
                End If
            End With
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
            Return False
        End Try
    End Function
#End Region

#Region "Event"

    ''' <summary>
    ''' Load danh sách mặt hàng khi chủng loại thay đổi
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Private Sub cboCategory_ValueChanged(sender As System.Object, e As System.EventArgs) Handles cboCategory.ValueChanged
        Try
            If cboCategory.IsItemInList Then
                LoadItem()
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Hiện form dlgNewCategory khi người dùng bấm vào nút tạo mới trong cboCategory trên form frmShipmentDetail
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Private Sub cboCategory_EditorButtonClick(sender As System.Object, e As Infragistics.Win.UltraWinEditors.EditorButtonEventArgs) Handles cboCategory.EditorButtonClick
        Try
            If e.Button.Key = "NewCategory" Then
                Using addCategory As New dlgNewCategory() With {.Owner = Me, .BusinessObject = Me.BusinessObject}
                    Dim currentCategory As String = cboCategory.Text
                    addCategory.ShowDialog()
                    If Not String.IsNullOrEmpty(addCategory.CategoryName) Then
                        BusinessObject.dsFMS.Category.Clear()
                        LoadCategory()
                        If ActionMode <> clscommon.ActionMode.View Then
                            cboCategory.SelectedText = addCategory.CategoryName
                        Else
                            cboCategory.SelectedText = currentCategory
                        End If
                    End If
                End Using
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Load danh sách người đại diện khi đối tác thay đổi
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 18/06/2012</remarks>
    Private Sub cboPartner_ValueChanged(sender As System.Object, e As System.EventArgs) Handles cboPartner.ValueChanged
        Try
            If cboPartner.IsItemInList Then
                LoadRepresentative()
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Lấy ra số lượng tồn kho của mặt hàng đang được chọn trong drpItem
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 12/06/2012</remarks>
    Private Sub drpItem_RowSelected(sender As System.Object, e As Infragistics.Win.UltraWinGrid.RowSelectedEventArgs) Handles drpItem.RowSelected
        Try
            If Not IsNothing(drpItem.SelectedRow) Then
                txtStore.Text = drpItem.SelectedRow.Cells("Quantity").Value
                If ActionMode <> clscommon.ActionMode.View Then
                    txtBuyPrice.Text = drpItem.SelectedRow.Cells("BuyPrice").Value
                    txtSellPrice.Text = drpItem.SelectedRow.Cells("SellPrice").Value
                    txtQuantity.Text = 1
                End If
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Thuế suất thay đổi thì tính toán lại tổng tiền sau thuế
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 12/06/2012</remarks>
    Private Sub rdbTaxRate_ValueChanged(sender As System.Object, e As System.EventArgs) Handles rdbTaxRate.ValueChanged
        Try
            With grdShipmentDetail
                If IsNumeric(.Rows.SummaryValues(0).Value) Then
                    txtTax.Text = (.Rows.SummaryValues(0).Value * rdbTaxRate.Value / 100).ToString
                    txtGrandTotal.Text = (.Rows.SummaryValues(0).Value + (.Rows.SummaryValues(0).Value * rdbTaxRate.Value / 100))
                End If
            End With
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Tăng hoặc giảm số lượng bằng right hay left button
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 12/06/2012</remarks>
    Private Sub txtQuantity_EditorButtonClick(sender As System.Object, e As Infragistics.Win.UltraWinEditors.EditorButtonEventArgs) Handles txtQuantity.EditorButtonClick
        Try
            With txtQuantity
                If e.Button.Key = "Add" Then
                    .Text = (Int16.Parse(.Text) + 1).ToString
                ElseIf e.Button.Key = "Subtract" Then
                    If Integer.Parse(.Text) <= 1 Then
                        .Text = 1
                    Else
                        .Text = (Int16.Parse(.Text) - 1).ToString
                    End If
                End If
            End With
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Nếu ô số lượng bị xóa thì trả lại giá trị là 1
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 12/06/2012</remarks>
    Private Sub txtQuantity_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtQuantity.TextChanged
        Try
            If String.IsNullOrEmpty(txtQuantity.Text) Then
                txtQuantity.Text = 1
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Tổng tiền thay đổi thì tính toán lại tổng tiền sau thuế
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 12/06/2012</remarks>
    Private Sub grdShipmentDetail_SummaryValueChanged(sender As System.Object, e As Infragistics.Win.UltraWinGrid.SummaryValueChangedEventArgs) Handles grdShipmentDetail.SummaryValueChanged
        Try
            With grdShipmentDetail
                If IsNumeric(.Rows.SummaryValues(0).Value) Then
                    txtTotalAmount.Text = .Rows.SummaryValues(0).Value
                    txtTax.Text = (.Rows.SummaryValues(0).Value * rdbTaxRate.Value / 100).ToString
                    txtGrandTotal.Text = (.Rows.SummaryValues(0).Value + (.Rows.SummaryValues(0).Value * rdbTaxRate.Value / 100))
                End If
            End With
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Khi một dòng trong grdShipmentDetail được chọn (Active) thì update giá trị trên các control khác
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 12/06/2012</remarks>
    Private Sub grdShipmentDetail_AfterRowActivate(sender As System.Object, e As System.EventArgs) Handles grdShipmentDetail.AfterRowActivate
        Try
            If ActionMode = clscommon.ActionMode.View Then
                If grdShipmentDetail.ActiveRow.Index = -1 Then
                    cboCategory.Clear()
                    drpItem.Text = String.Empty
                    txtStore.Clear()
                    txtQuantity.Text = "0"
                    txtBuyPrice.Text = String.Empty
                    txtSellPrice.Text = String.Empty
                Else
                    With BusinessObject.dsFMS
                        cboCategory.Value = grdShipmentDetail.ActiveRow.Cells(.Item.CategoryIDColumn.ColumnName).Value
                        drpItem.Value = grdShipmentDetail.ActiveRow.Cells(.Item.ItemIDColumn.ColumnName).Value
                        txtQuantity.Text = grdShipmentDetail.ActiveRow.Cells(.ShipmentDetail.ShipmentDetailQuantityColumn.ColumnName).Value
                        txtSellPrice.Text = grdShipmentDetail.ActiveRow.Cells(.ShipmentDetail.ShipmentDetailSellPriceColumn.ColumnName).Value
                        txtBuyPrice.Text = grdShipmentDetail.ActiveRow.Cells(.ShipmentDetail.ShipmentDetailBuyPriceColumn.ColumnName).Value
                    End With
                End If
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Thêm một mặt hàng vào phiếu nhập kho
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 12/06/2012</remarks>
    Private Sub btnAddShipmentDetail_Click(sender As System.Object, e As System.EventArgs) Handles btnAddShipmentDetail.Click
        Try
            With bsShipmentDetail
                If AddShipmentDetail() Then
                    With BusinessObject.dsFMS.Item
                        If IsDBNull(bsShipmentDetail.Current(.ItemAliasColumn.ColumnName)) Then
                            bsShipmentDetail.Current(.ItemAliasColumn.ColumnName) = drpItem.SelectedRow.Cells(.ItemAliasColumn.ColumnName).Value
                            bsShipmentDetail.Current(.ItemNameColumn.ColumnName) = drpItem.SelectedRow.Cells(.ItemNameColumn.ColumnName).Value
                        End If
                    End With
                    cboCategory.Focus()
                    txtQuantity.Text = 1
                End If
            End With
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub
#End Region
End Class
